package ui.activity.login.presenter;


import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.msd.ocr.idcard.LibraryInitOCR;

import net.ck.hzq.MainActivity;
import net.ck.hzq.TobuyApplication;

import javax.inject.Inject;

import Bean.LoginBean;
import Bean.LoginParamBean;
import Utils.SharedPrefStore;
import base.BaseBiz;
import dao.PutYmInput;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.login.ForgetPwdAct;
import ui.activity.login.RegisterAct;
import ui.activity.login.biz.LoginBiz;
import ui.activity.login.contract.LoginContract;
import util.StringUtils;

/**
 * Created by chenjian on 19/2/26
 */

public class LoginPresenter implements LoginContract.Presenter {

    LoginContract.View view;
    LoginBiz loginBiz;


    @Inject
    public LoginPresenter(LoginContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        loginBiz = (LoginBiz) biz;
    }


    @Override
    public void getLoginInfo(String customName, String passWord) {
        if (StringUtils.isEmpty(customName) || StringUtils.isEmpty(passWord)) {
            view.showMsg("请填写登录信息");
            return;
        }
//        if (!CheckPhoneNumberUtils.isMobileNO(customName)) {
//            view.showMsg("请填写正确的手机号");
//            return;
//        }
        view.loading(true);
        LoginParamBean loginParamBean = new LoginParamBean();
        loginParamBean.setCustomName(customName);
        loginParamBean.setPassWord(passWord);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(loginParamBean));
        loginBiz.getLogin(requestBody, new BaseBiz.Callback<LoginBean.DataBean>() {
            @Override
            public void onSuccess(LoginBean.DataBean info) {
                SharedPrefStore store = new SharedPrefStore();
                store.putString("name", info.getCustom().getName());
                store.putString("customCode", info.getInfo().getCustomCode());
                store.putString("id", info.getInfo().getId() + "");
                store.putString("realname", info.getInfo().getRealName());
                store.putString("isRealNameAuthenticationed", info.getInfo().getIsRealNameAuthenticationed() + "");
                store.putString("phonenumber", info.getInfo().getPhoneNumber());
                store.putString("headImage", info.getInfo().getHeadImage());
                store.putString("login_account", customName);
                store.putString("login_password", passWord);
                TobuyApplication.customId = info.getInfo().getId()+"";
                TobuyApplication.token = info.getCustom().getToken();

                PutYmInput param = new PutYmInput();
                param.customId=info.getInfo().getId()+"";
                param.token = TobuyApplication.youmengtoken;
                param.terminal = "2";
                RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(param));

                loginBiz.putUmtoken(requestBody, new BaseBiz.Callback<Boolean>() {
                    @Override
                    public void onSuccess(Boolean aBoolean) {
                        Log.e("fjs","后台注册成功：deviceToken：-------->  " );
                    }

                    @Override
                    public void onFailure(Boolean aBoolean) {

                    }
                });
                view.loading(false);
                view.startAct(MainActivity.class, null);
                view.loading(false);
                view.finishAct();
            }

            @Override
            public void onFailure(LoginBean.DataBean info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void toRegister() {
        view.startAct(RegisterAct.class, null);
    }

    @Override
    public void toForget() {
        view.startAct(ForgetPwdAct.class, null);
    }


}
